//related to login.ui
var page = sm("do_Page");
var app = sm("do_App");
var anim = require("anim");
var nf = sm("do_Notification");
var buttonA = mm("do_Animation", "BUTTONTOUCHDOWNS", "app");
var cacher = sm("do_DataCache");
var listdata = mm("do_ListData");
var service_url = sm("do_Global");
var url  = service_url.getMemory("service_url");
var url_bac  = service_url.getMemory("service_url_bac");

var textf1 = ui("do_TextField_1");
var textf2 = ui("do_TextField_2");





//物理按键返回
page.on("back", function(data) {
	app.closePage();
})

var do_Button_3 = ui("do_Button_3");

do_Button_3.on("touch", function() {
	app.openPage("source://view/logReg/loginmultiuser.ui");
})

//登录按下状态
var animLogYes = mm("do_Animator");
var propsLY = {width:120,x:240,bgColor:"A6EBFFFF"};
animLogYes.append(360,propsLY,"EaseOut");
//恢复状态
var animLogB = mm("do_Animator");
var propsB = {width:580,x:10,bgColor:"3BD1FFFF"};
animLogB.append(360,propsB,"EaseOut");

var lsuccess = 1; //状态
var logbtn = ui("login");
var probar1=ui("do_ProgressBar_1");
logbtn.on("touch",function(){
	if(textf1.text == ""&&textf2.text==""){
		nf.toast("输入账号|密码");
		textf1.setFocus(true);
	}else if(textf1.text == ""){
		nf.toast("输入账号");
		textf1.setFocus(true);
	}else if(textf2.text==""){
		nf.toast("输入密码");
		textf2.setFocus(true);
	}else{
		login();
	}
	page.hideKeyboard();
});
/* var do_ScrollView_1 = ui("do_ScrollView_1");

do_ScrollView_1.on("scroll", function() {
	app.openPage("source://view/logReg/login_1.ui");
})*/

function login(){
	var http = mm("do_Http");
	http.method = "POST";
	http.timeout = 30000;
	http.setRequestHeader("X-Requested-With", "XMLHttpRequest");
	http.url = url+"index.php?s=/admin/login/login";
	http.contentType = "application/x-www-form-urlencoded";
	http.body = "userName="+textf1.text+"&password="+textf2.text;

	http.on("success", function(data) {
		//nf.toast(data);
		if(data.code ==1){
			nf.toast(data.message);
			var re = http.getResponseHeader("Set-Cookie");
			cacher.saveData("token", re);
			app.openPage("source://view/index.ui");
		}else{
			nf.toast(data.message);
		}
		
	});
	http.request();
}



